package com.sky.service.impl;

import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.service.WorkService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class WorkServiceImpl implements WorkService {

    @Autowired
    private ReportServiceImpl reportService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO businessData(LocalDate begin, LocalDate end) {
        UserReportVO userReportVO = reportService.userStatistics(begin, end);
        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(begin, end);
        OrderReportVO orderReportVO = reportService.ordersStatistics(begin, end);

        return BusinessDataVO.builder()
                .newUsers(userReportVO.getNewUser())
                .unitPrice(Double.valueOf(turnoverReportVO.getTotalTurnover())/ orderReportVO.getValidOrderCount())
                .validOrderCount(orderReportVO.getValidOrderCount())
                .orderCompletionRate(orderReportVO.getOrderCompletionRate())
                .turnover(Double.valueOf(turnoverReportVO.getTotalTurnover()))
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders(LocalDate begin, LocalDate end) {
        OrderOverViewVO OrderOverViewVO =new OrderOverViewVO(0,0,0,0,0);

        List<Map<String, Object>> list = orderMapper.findall(begin, end);

        for (Map<String, Object> map : list) {
            Long count = (Long) map.get("count");  // 强制转换为 Long 类型
            Integer status = (Integer) map.get("status");  // 强制转换为 Integer 类型

            if (status != null) {
                if (status == 2) {
                    OrderOverViewVO.setWaitingOrders(count != null ? count.intValue() : 0);
                }
                if (status == 3) {
                    OrderOverViewVO.setDeliveredOrders(count != null ? count.intValue() : 0);
                }
                if (status == 5) {
                    OrderOverViewVO.setCompletedOrders(count != null ? count.intValue() : 0);
                }
                if (status == 6) {
                    OrderOverViewVO.setCancelledOrders(count != null ? count.intValue() : 0);
                }
            }
        }
        OrderOverViewVO.setAllOrders(orderMapper.countbyday(begin, end));
        return OrderOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        List<Map<String, Object>> ls= setmealMapper.overviewSetmeals();
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder().sold(0).discontinued(0).build();
        for (Map<String, Object> map : ls) {
            Integer status = (Integer) map.get("status");
            Long count = (Long) map.get("count");

            if (status==0) {
                setmealOverViewVO.setDiscontinued(count != null ? count.intValue() : 0);
            }
            if (status==1) {
                setmealOverViewVO.setSold(count != null ? count.intValue() : 0);
            }
        }
        return setmealOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        List<Map<String, Object>> ls= dishMapper.overviewDishes();
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        for (Map<String, Object> map : ls) {
            Integer status = (Integer) map.get("status");
            Long count = (Long) map.get("count");

            if (status==0) {
                dishOverViewVO.setDiscontinued(count != null ? count.intValue() : 0);
            }
            if (status==1) {
                dishOverViewVO.setSold(count != null ? count.intValue() : 0);
            }
        }

        return dishOverViewVO;
    }
}
